﻿using _03_Tank.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03_Tank
{
    internal class EnemyTank : Movething
    {
        private int ChangeDirSpeed { get; set; }
        private int changeDirCount = 0;
        private int AttackSpeed { get; set; }
        private int attackCount = 0;
        private Random rd = new Random();

        public EnemyTank(int x, int y, int speed, Bitmap bmpDown, Bitmap bmpUp, Bitmap bmpLeft, Bitmap bmpRight)
        {
            
            BitmapDown = bmpDown;
            BitmapUp = bmpUp;
            BitmapLeft = bmpLeft;
            BitmapRight = bmpRight;

            X = x;
            Y = y;
            Speed = speed;
            Dir = Direction.Down;
            AttackSpeed = 60;
            ChangeDirSpeed = 70;
        }

        public override void Update()
        {
            MoveCheck();
            Move();
            AttackCheck();
            AutoChangeDirection();
            base.Update();
        }

        private void MoveCheck()
        {
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Height + Speed > 450)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Width + Speed > 450)
                {
                    ChangeDirection(); return;
                }
            }


            Rectangle rectangle = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rectangle.Y -= Speed;
                    break;
                case Direction.Down:
                    rectangle.Y += Speed;
                    break;
                case Direction.Left:
                    rectangle.X -= Speed;
                    break;
                case Direction.Right:
                    rectangle.X += Speed;
                    break;
            }

            if (GameObjectManager.IsCollidedWall(rectangle) != null)
            {
                ChangeDirection();
                return;
            }

            if (GameObjectManager.IsCollidedSteel(rectangle) != null)
            {
                ChangeDirection();
                return;
            }

            if (GameObjectManager.IsCollidedBoss(rectangle))
            {
                ChangeDirection();
                return;
            }
        }

        private void AutoChangeDirection()
        {
            changeDirCount++;
            if (changeDirCount < ChangeDirSpeed) return;
            ChangeDirection();
            changeDirCount = 0;
        }

        private void ChangeDirection()
        {
            while (true)
            {
                Direction dir = (Direction)rd.Next(0, 4);
                if (dir == Dir)
                {
                    continue;
                }
                else
                {
                    Dir = dir; break;
                }
            }
            MoveCheck();
        }

        private void Move()
        {
            //if (!IsMoving) return;

            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                default:
                    break;
            }
        }

        public override void DrawSelf()
        {
            lock (_lock)
            {
                base.DrawSelf();
            }
            
        }

        private void AttackCheck()
        {
            attackCount++;
            if (attackCount < AttackSpeed) return;
            Attack();
            attackCount = 0;
        }

        private void Attack()
        {
            int x = X;
            int y = Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + Width / 2;
                    break;
                case Direction.Down:
                    x = x + Width / 2;
                    y += Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.Right:
                    x += Width;
                    y = y + Height / 2;
                    break;

            }
            GameObjectManager.CreateBullet(x, y, Tag.EnemyTank, Dir);
        }
    }
}
